package oocis.util;

import java.io.*;
import java.lang.Process;

public class ExtCall {
	
	private static String command="dir";
	static Integer a = new Integer(0);
	public ExtCall(String _command){
		command = _command;
	}
	
	public static int exec() throws IOException{
		Process p = Runtime.getRuntime().exec("cmd /c " + command);
		
		InputStreamReader isr = new InputStreamReader(p.getInputStream());
		BufferedReader br = new BufferedReader(isr);
		
		RandomAccessFile raf = new RandomAccessFile("d:\\log.log", "rw");
		raf.seek(raf.length());
		
		raf.write(command.getBytes());
		raf.write(13);
		
		String l;
		while ((l = br.readLine()) != null){
			l = l + "\n";
			System.out.println(l);
//			fw.append(l);
			raf.write(l.getBytes());
		}
		
		
		isr = new InputStreamReader(p.getErrorStream());
		br = new BufferedReader(isr);
		while ((l = br.readLine()) != null){
			l = l + "\n";
			System.out.println(l);
//			fw.append(l);
			raf.write(l.getBytes());
		}
//		fw.close();
		raf.close();
		return p.exitValue();		
	}

	public static int exec(String _command) throws IOException{
		Process p = Runtime.getRuntime().exec("cmd /c " + _command);
		
		InputStreamReader isr = new InputStreamReader(p.getInputStream());
		BufferedReader br = new BufferedReader(isr);
		a++;
		String s = a.toString();
		FileOutputStream fos = new FileOutputStream(new java.io.File("d:\\2000\\tmp"+s+".txt"));
		
//		FileWriter fw = new FileWriter("d:\\log.log");
		java.io.RandomAccessFile raf = new RandomAccessFile("d:\\log.log","rw");
		raf.seek(raf.length());
		
		raf.write(_command.getBytes());
		raf.write(13);
		
		String l;
		fos.write(_command.getBytes());
		while ((l = br.readLine()) != null){
			l = l + "\n";
			fos.write(l.getBytes());
//			fw.append(l);
			raf.write(l.getBytes());
//			B
		}
		fos.close();
		
		isr = new InputStreamReader(p.getErrorStream());
		br = new BufferedReader	(isr);
		fos = new FileOutputStream(new java.io.File("d:\\2000\\tmp2.txt"));
		
		while ((l = br.readLine()) != null){
			l = l + "\n";
			fos.write(l.getBytes());
			raf.write(l.getBytes());
//			fw.append(l);
		}		
		fos.close();
		raf.close();
//		fw.close();
		
		return p.exitValue();
		
	}
}
